/**
 * 
 */
package ru.olympus.antonyhands.ao.client.widgets;

import ru.olympus.antonyhands.ao.client.AOHelperServiceAsync;
import ru.olympus.antonyhands.ao.shared.util.StringUtilities;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author AntonyHands
 * @since 14.02.2012
 */
public class AddItemBaseDialogBox extends DialogBox
{
    private String _itemType;
    private AOHelperServiceAsync _service;
    private Label errorLabel;
    private TextBox codeField;
    private TextBox titleField;
    private Button okButton;
    private Button closeButton;

    public AddItemBaseDialogBox(String itemType, AOHelperServiceAsync service, String title)
    {
        _itemType = itemType;
        _service = service;
        setText(title);

        setAnimationEnabled(true);

        VerticalPanel dialogVPanel = new VerticalPanel();
        dialogVPanel.addStyleName("dialogVPanel");

        errorLabel = new Label();
        errorLabel.setStyleName("serverResponseLabelError");
        dialogVPanel.add(errorLabel);

        Grid table = new Grid(2, 2);
        table.setHTML(0, 0, "Код");
        table.setHTML(1, 0, "Название");
        dialogVPanel.add(table);

        codeField = new TextBox();
        table.setWidget(0, 1, codeField);

        titleField = new TextBox();
        table.setWidget(1, 1, titleField);

        HorizontalPanel buttonsPanel = new HorizontalPanel();
        buttonsPanel.addStyleName("dialogHPanel");
        dialogVPanel.add(buttonsPanel);

        okButton = new Button("OK");
        closeButton = new Button("Отмена");

        okButton.addClickHandler(new ClickHandler()
        {
            public void onClick(ClickEvent event)
            {
                errorLabel.setText("");
                if (StringUtilities.isEmpty(codeField.getText()))
                {
                    errorLabel.setText("Ошибка: код должен быть заполнен!");
                    center();
                    return;
                }
                else if (StringUtilities.isEmpty(titleField.getText()))
                {
                    errorLabel.setText("Ошибка: название должно быть заполнено!");
                    center();
                    return;
                }
                okButton.setEnabled(false);
                closeButton.setEnabled(false);
                _service.addItemBase(_itemType, codeField.getText(), titleField.getText(), new AsyncCallback<String>()
                {
                    @Override
                    public void onFailure(Throwable caught)
                    {
                        errorLabel.setText("Ошибка работы с сервером");
                        okButton.setEnabled(true);
                        closeButton.setEnabled(true);
                        center();
                    }

                    @Override
                    public void onSuccess(String result)
                    {
                        okButton.setEnabled(true);
                        closeButton.setEnabled(true);
                        if (StringUtilities.isEmpty(result))
                        {
                            cleanFields();
                            hide();
                        }
                        else
                        {
                            errorLabel.setText(result);
                            center();
                        }
                    }
                });
            }
        });
        buttonsPanel.add(okButton);

        closeButton.addClickHandler(new ClickHandler()
        {
            public void onClick(ClickEvent event)
            {
                cleanFields();
                hide();
            }
        });
        buttonsPanel.add(closeButton);

        setWidget(dialogVPanel);
    }

    private void cleanFields()
    {
        errorLabel.setText("");
        codeField.setText("");
        titleField.setText("");
    }
}
